//
//  CRArticleCollectionViewCell.m
//  NewAge
//
//  Created by 王鑫 on 15/12/3.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRArticleCollectionViewCell.h"

@implementation CRArticleCollectionViewCell

#pragma mark Method init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}



#pragma mark - Method createView

- (void)createView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.delegate = self;
    [self.contentView addSubview:self.scrollView];
    
    self.contentDetailView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentDetailView];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.dateLabel = [[UILabel alloc] init];
    self.dateLabel.font = [UIFont fontWithName:@"ArialUnicodeMS" size:15];
    [self.contentDetailView addSubview:self.dateLabel];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:21.0];
    [self.contentDetailView addSubview:self.titleLabel];
    
    self.authorLabel = [[UILabel alloc] init];
    self.authorLabel.textColor = [UIColor colorWithRed:0.725f green:0.729f blue:0.733f alpha:1.00f];
    self.authorLabel.font = [UIFont boldSystemFontOfSize:18.0];
    [self.contentDetailView addSubview:self.authorLabel];
    
    self.contentTextView = [[UITextView alloc] init];
    self.contentTextView.userInteractionEnabled = NO;
    [self.contentTextView sizeToFit];
    [self.contentDetailView addSubview:self.contentTextView];
    
    self.textView = [[UITextView alloc] init];
    self.textView = (UITextView *)self.contentDetailView;
}



#pragma mark - Method layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(0);
        make.width.mas_equalTo(self.contentView.mas_width);
    }];
    self.scrollView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    
    [self.contentDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView.mas_width);
    
    }];

    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_top).with.offset(10);
        make.left.equalTo(self.scrollView.mas_left).with.offset(10);
        make.right.equalTo(self.scrollView.mas_right).with.offset(10);
        make.height.mas_equalTo(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dateLabel.mas_bottom).with.offset(10);
        make.left.equalTo(self.scrollView.mas_left).with.offset(10);
        make.width.mas_equalTo(WIDTH - 20);
        make.height.mas_equalTo(40);
    }];
    
    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(10);
        make.left.equalTo(self.scrollView.mas_left).with.offset(10);
    }];
    
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.authorLabel.mas_bottom).with.offset(10);
        make.left.equalTo(self.contentDetailView.mas_left).with.offset(10);
        make.right.equalTo(self.contentDetailView.mas_right).with.offset(-10);
        make.height.mas_equalTo(self.contentTextView.contentSize.height * 1.8);
    }];
    
    [self.contentDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentTextView.mas_bottom).with.offset(50);
    }];
    
}



#pragma mark - Method setModel

- (void)setModel:(CRArticleModel *)model {
    if (_model != model) {
        _model = model;
        
        NSString *dayStr = [self.model.strContMarketTime substringFromIndex:self.model.strContMarketTime.length - 2];
        
        NSString *monthStr = [self.model.strContMarketTime substringWithRange:NSMakeRange(self.model.strContMarketTime.length - 5, 2)];
        
        NSString *yearStr = [self.model.strContMarketTime substringToIndex:4];
        
        NSInteger month = [monthStr integerValue];
        switch (month) {
            case 1:
                self.dateLabel.text = [NSString stringWithFormat:@"January %@,%@", dayStr, yearStr];
                break;
            case 2:
                self.dateLabel.text = [NSString stringWithFormat:@"February %@,%@", dayStr, yearStr];
                break;
            case 3:
                self.dateLabel.text = [NSString stringWithFormat:@"March %@,%@", dayStr, yearStr];
                break;
            case 4:
                self.dateLabel.text = [NSString stringWithFormat:@"April %@,%@", dayStr, yearStr];
                break;
            case 5:
                self.dateLabel.text = [NSString stringWithFormat:@"May %@,%@", dayStr, yearStr];
                break;
            case 6:
                self.dateLabel.text = [NSString stringWithFormat:@"June %@,%@", dayStr, yearStr];
                break;
            case 7:
                self.dateLabel.text = [NSString stringWithFormat:@"July %@,%@", dayStr, yearStr];
                break;
            case 8:
                self.dateLabel.text = [NSString stringWithFormat:@"August %@,%@", dayStr, yearStr];
                break;
            case 9:
                self.dateLabel.text = [NSString stringWithFormat:@"September %@,%@", dayStr, yearStr];
                break;
            case 10:
                self.dateLabel.text = [NSString stringWithFormat:@"October %@,%@", dayStr, yearStr];
                break;
            case 11:
                self.dateLabel.text = [NSString stringWithFormat:@"November %@,%@", dayStr, yearStr];
                break;
            case 12:
                self.dateLabel.text = [NSString stringWithFormat:@"December %@,%@", dayStr, yearStr];
                break;
                
            default:
                break;
        }

        self.titleLabel.text = self.model.strContTitle;
        self.authorLabel.text = self.model.strContAuthor;
        
        NSString *str = self.model.strContent;
        str = [str stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        self.attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:5];
        [self.attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];
        [self.attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, str.length)];
//        UILabel *label = [[UILabel alloc] init];
//        label.numberOfLines = 0;
//        label.font = [UIFont systemFontOfSize:17];
//        label.attributedText = self.attributedString;
//        CGSize size = CGSizeMake(WIDTH - 30, 30000);
//        CGSize labelSize = [label sizeThatFits:size];
//        self.contentHeihgt = labelSize.height;
//        NSLog(@"conte%@", self.attributedString);
        self.contentTextView.attributedText = self.attributedString;
        
    }
}

//-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(self.textView.contentSize.height);
////        NSLog(@"%ld", self.textView.contentSize.height);
//    }];
//    
//}


@end



